package com.shsc.example.project;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import java.util.List;
import java.util.Map;

class ContactsAdapter extends RecyclerView.Adapter<ContactsAdapter.MyViewHolder> {
    private List<Map<String, String>> stringList;

    public void setDatas(List<Map<String, String>> stringList) {
        this.stringList = stringList;
        notifyDataSetChanged();
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_rv_test, parent, false);
        MyViewHolder vh = new MyViewHolder(v);
        return vh;
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, int position) {
        Map<String, String> contactsMap = stringList.get(position);
        holder.tvContacts.setText(String.format("%s,%s", contactsMap.get("name"), contactsMap.get("phone")));
    }


    @Override
    public int getItemCount() {
        return stringList == null ? 0 : stringList.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        public TextView tvContacts;

        public MyViewHolder(View itemView) {
            super(itemView);
            tvContacts = (TextView) itemView.findViewById(R.id.tv_contacts);
        }
    }
}